package cn.zucc.edu.handyzucc.controller;

import cn.zucc.edu.handyzucc.entity.Teacher;
import cn.zucc.edu.handyzucc.entity.User;
import cn.zucc.edu.handyzucc.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @RequestMapping(value = "getAllTeachers", method = RequestMethod.GET)
    private Map<String, Object> getAllTeachers() {
        Map<String, Object> modelMap = new HashMap<>();
        List<Teacher> teachers = teacherService.getAllTeachers();
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", teachers);
        return modelMap;
    }

    @RequestMapping(value = "searchTeachers", method = RequestMethod.POST)
    private Map<String, Object> searchTeachers(String keyword) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Teacher> teachers = teacherService.searchTeachers(keyword);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", teachers);
        return modelMap;
    }

    @RequestMapping(value = "getTeacherDetail", method = RequestMethod.POST)
    private Map<String, Object> getTeacherDetail(Integer teacher_id) {
        Map<String, Object> modelMap = new HashMap<>();
        Teacher teacher = teacherService.getTeacherDetail(teacher_id);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", teacher);
        return modelMap;
    }

    @RequestMapping(value = "deleteTeacher", method = RequestMethod.POST)
    private Map<String, Object> deleteTeacher(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        if (teacherService.deleteTeacher(Integer.parseInt(map.get("teacher_id").toString())))
            modelMap.put("code", "0");
        else
            modelMap.put("code", "-1");
        return modelMap;
    }


    @RequestMapping(value = "updateTeacher", method = RequestMethod.POST)
    private Map<String, Object> updateTeacher(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        Teacher teacher = new Teacher(Integer.parseInt(map.get("teacher_id").toString()),map.get("teacher_name").toString(), map.get("teacher_office").toString(), map
                .get("teacher_phone").toString(), map.get("teacher_email").toString(), map.get("teacher_wechat").toString(), map.get("teacher_qq").toString());
        if (teacherService.updateTeacher(teacher))
            modelMap.put("code", "0");
        else
            modelMap.put("code", "-1");
        return modelMap;
    }

    @RequestMapping(value = "batchSignup", method = RequestMethod.POST)
    private Map<String, Object> batchSignup(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        Map<String, Object> modelMap = new HashMap<>();
        List<Teacher> teachers = teacherService.batchSignup(multipartFile);
        modelMap.put("code", "0");
        modelMap.put("data", teachers);
        return modelMap;

    }
}
